package com.caoyuqi.picture.controll;

import com.caoyuqi.picture.model.User;
import com.caoyuqi.picture.service.CrudService;
import com.caoyuqi.picture.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
@Controller
public class UserControl {
    @Autowired
    LoginService loginService;
    @Autowired
    CrudService crudService;
    @PostMapping("/sqlUser")
    String sqlUser(User user, Model model, String cr, HttpSession session) {

        if(cr.equals("添加账号"))
        {
            if(crudService.sfcz(user.getUsername())!=null)
            {
                ArrayList<com.caoyuqi.picture.model.User> users = loginService.QueryAll();
                model.addAttribute("users",users);
                model.addAttribute("msg","该账户已存在");


            }else {

                crudService.InsertInToUser(user.getUsername(),user.getPwd(),user.getAdmin());
                ArrayList<com.caoyuqi.picture.model.User> users = loginService.QueryAll();
                model.addAttribute("users",users);

            }
        }else {
            if(crudService.sfcz(user.getUsername())!=null)
            {
                crudService.DeleteUser(user.getUsername());
                ArrayList<com.caoyuqi.picture.model.User> users = loginService.QueryAll();
                model.addAttribute("users",users);



            }else
            {
                ArrayList<com.caoyuqi.picture.model.User> users = loginService.QueryAll();
                model.addAttribute("users",users);
                model.addAttribute("msg","该账户不存在");
            }
        }

        return "adminPage";
    }


}
